/*
 * VHClubInfoContextMenu.java
 * Created on 18 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.menu;

import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.VHMainController;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.ui.panels.VHPanelClub;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Creates a context menu that displays links to the club info pages of the teams involved
 * in a match.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHMatchInfoContextMenu extends VHGridContextMenu
{
  private String   d_strClubHomePropertiesPrefix;
  private String   d_strClubAwayPropertiesPrefix;
  private MenuItem d_miClubHomeInfo;
  private MenuItem d_miClubAwayInfo;

  /**
   * Creates a new context menu that displays a link to the club info of the clubs of the
   * last clicked match.
   *
   * @param controller the VH display controller
   * @param grid the grid to which to attach the context menu
   * @param strClubHomePropertiesPrefix the prefix that needs to be added in front of the
   *          typical club properties (id, name, country).
   * @param strClubAwayPropertiesPrefix the prefix that needs to be added in front of the
   *          typical club properties (id, name, country).
   */
  public VHMatchInfoContextMenu( VHController controller, Grid<VHJsData> grid, String strClubHomePropertiesPrefix,
                                 String strClubAwayPropertiesPrefix )
  {
    super( controller, grid );

    d_strClubHomePropertiesPrefix = strClubHomePropertiesPrefix;
    d_strClubAwayPropertiesPrefix = strClubAwayPropertiesPrefix;

    d_miClubHomeInfo = new MenuItem( VHMain.getConstants().ctxMenuShowHomeClubInfo(), getDefaultSelectionListener() );
    add( d_miClubHomeInfo );

    d_miClubAwayInfo = new MenuItem( VHMain.getConstants().ctxMenuShowAwayClubInfo(), getDefaultSelectionListener() );
    add( d_miClubAwayInfo );
  }

  @Override
  protected void customizeMenuBeforeShowing( VHJsData modelLastClicked )
  {
    d_miClubHomeInfo.setToolTip( VHMain.getMessages().ctxMenuShowClubInfo(
        modelLastClicked.getString( d_strClubHomePropertiesPrefix + "name" ) ) );

    d_miClubAwayInfo.setToolTip( VHMain.getMessages().ctxMenuShowClubInfo(
        modelLastClicked.getString( d_strClubAwayPropertiesPrefix + "name" ) ) );
  }

  @Override
  protected void menuItemSelected( MenuItem mi )
  {
    String strClubId = getModelLastClicked().getString( d_strClubHomePropertiesPrefix + "id" );
    String strClubName = getModelLastClicked().getString( d_strClubHomePropertiesPrefix + "name" );

    if ( mi.equals( d_miClubHomeInfo ) )
    {
      strClubId = getModelLastClicked().getString( d_strClubHomePropertiesPrefix + "id" );
      strClubName = getModelLastClicked().getString( d_strClubHomePropertiesPrefix + "name" );
    }
    else if ( mi.equals( d_miClubAwayInfo ) )
    {
      strClubId = getModelLastClicked().getString( d_strClubAwayPropertiesPrefix + "id" );
      strClubName = getModelLastClicked().getString( d_strClubAwayPropertiesPrefix + "name" );
    }
    else
      super.menuItemSelected( mi );

    ( (VHMainController) getController() ).displayPanel( VHPanelClub.get( getController(), strClubId ),
        VHMain.getMessages().clubPanelHeader( strClubName ), VHMain.ICON_CLUB );
  }
}
